#!/usr/bin python3
# -*- coding: utf-8 -*-
"""  任务信息表dao层  """
from gtm_server.models.tasks import TaskModel, StepModel
from gtm_server.common.constant import TaskStatusKeys


def insert_task_in_table_dao(session, task_info):
    """
    task_info:{
        id: uuid,
        name: "",
        ori_lat: 45.567,
        ori_lng: 116.354,
        de_lat: 46.123,
        de_lng: 118.125,
        create_time: ,
        brief: "",
        pre_duration: 11568.5(s)
        index: 4,
        steps: 2,
        distance: 54855
    }
    记录一条生成的任务信息dao层
    """
    try:
        print(task_info)
        new_task = TaskModel(
            id = task_info.get("id"),
            name = task_info.get("name"),
            ori_lat = task_info.get("ori_lat"),
            ori_lng = task_info.get("ori_lng"),
            de_lat = task_info.get("de_lat"),
            de_lng = task_info.get("de_lng"),
            create_time = task_info.get("create_time"),
            brief = task_info.get("brief"),
            pre_duration = task_info.get("pre_duration"),
            index = task_info.get("index"),
            steps = task_info.get("steps"),
            distance = task_info.get("distance"),
        )

        session.add(new_task)
        session.commit()

    except Exception as ex:
        raise ex
        # return TaskStatusKeys.FAILED

    return TaskStatusKeys.SUCCESS


def insert_step_in_table_dao(session, step_info):
    """
    step_info:{
        id: uuid,
        task_id: uuid,
        leg_index: 4,
        direction: 5,
        distance: 5896,
        road_name: 明德路，
        road_type: 其他路，
        start_lat: 45.567,
        start_lng: 116.354,
        end_lat: 46.123,
        end_lng: 118.125,
        path: ""
    }
    记录一条生成的任务分段信息dao层
    """
    try:
        new_step = StepModel(
            id = step_info.get("id"),
            task_id = step_info.get("task_id"),
            leg_index = step_info.get("leg_index"),
            direction = step_info.get("direction"),
            distance = step_info.get("distance"),
            road_name = step_info.get("road_name"),
            road_type = step_info.get("road_type"),
            start_lat = step_info.get("start_lat"),
            start_lng = step_info.get("start_lng"),
            end_lat = step_info.get("end_lat"),
            end_lng = step_info.get("end_lng"),
            path = step_info.get("path"),
            no = step_info.get("no"),
        )

        session.add(new_step)
        session.commit()

    except Exception as ex:
        raise ex
        # return TaskStatusKeys.FAILED

    return TaskStatusKeys.SUCCESS


def get_all_task(session):
    """
    获取所有任务信息(主要信息) 获取位置: task_table
    """
    try:
        tasks = session.query(TaskModel).all()
        if tasks:
            return [task for task in tasks]
        else:
            return []
    except Exception as ex:
        print(ex)
        return TaskStatusKeys.FAILED
